package com.cdu.ai_intelligent_teaching_and_training_system.controller;

import com.cdu.ai_intelligent_teaching_and_training_system.dto.ApiResponse;
import com.cdu.ai_intelligent_teaching_and_training_system.entity.Student;
import com.cdu.ai_intelligent_teaching_and_training_system.entity.Teacher;
import com.cdu.ai_intelligent_teaching_and_training_system.entity.User;
import com.cdu.ai_intelligent_teaching_and_training_system.mapper.StudentMapper;
import com.cdu.ai_intelligent_teaching_and_training_system.mapper.TeacherMapper;
import com.cdu.ai_intelligent_teaching_and_training_system.service.UserService;
import com.cdu.ai_intelligent_teaching_and_training_system.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
@CrossOrigin(origins = "*")
public class UserController {
    
    @Autowired
    private UserService userService;
    
    @Autowired
    private TeacherMapper teacherMapper;
    
    @Autowired
    private StudentMapper studentMapper;
    
    @Autowired
    private JwtUtil jwtUtil;
    
    @GetMapping("/profile")
    public ResponseEntity<ApiResponse<Map<String, Object>>> getUserProfile(@RequestHeader("Authorization") String authHeader) {
        try {
            String token = authHeader.replace("Bearer ", "");
            // 从token中提取用户信息
            String username = jwtUtil.extractUsername(token);
            User user = userService.findByUsername(username).orElse(null);
            
            if (user == null) {
                return ResponseEntity.ok(ApiResponse.error("用户不存在"));
            }
            
            Map<String, Object> profile = new HashMap<>();
            profile.put("userId", user.getUserId());
            profile.put("username", user.getUsername());
            profile.put("email", user.getEmail());
            profile.put("role", user.getRole());
            
            // 根据角色获取详细信息
            if ("teacher".equals(user.getRole())) {
                Teacher teacher = teacherMapper.selectByUserId(user.getUserId());
                if (teacher != null) {
                    profile.put("name", teacher.getName());
                    profile.put("department", teacher.getDepartment());
                }
            } else if ("student".equals(user.getRole())) {
                Student student = studentMapper.selectByUserId(user.getUserId());
                if (student != null) {
                    profile.put("name", student.getName());
                    profile.put("major", student.getMajor());
                    profile.put("enrollmentYear", student.getEnrollmentYear());
                }
            }
            
            return ResponseEntity.ok(ApiResponse.success(profile));
        } catch (Exception e) {
            return ResponseEntity.ok(ApiResponse.error("获取用户信息失败：" + e.getMessage()));
        }
    }
} 